<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>layui</title>
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport"
	content="width=device-width, initial-scale=1, maximum-scale=1">
<link rel="stylesheet" href="../../../static/layui2/css/layui.css"
	media="all" />
</head>
<body>
	<fieldset class="layui-elem-field layui-field-title"
		style="margin-top: 20px;">
		<legend>用户注册功能</legend>
	</fieldset>

	<form class="layui-form" lay-filter="example">
		<br class="layui-form-item">
		<div class="layui-upload">
			<label class="layui-form-label">用户头像</label>
			<input type="text" name="headImg"  id="headImg" class="layui-input newsName"  placeholder="请添加头像" style="display: none">
            <div class="layui-upload-list">
            	<img class="layui-upload-img" id="uploadFile"  height="80px" width="80px" src="" style="cursor:pointer;">
			</div>
		</div>   
		<div class="layui-form-item">
			<label class="layui-form-label">账号</label>
			<div class="layui-input-block">
				<input type="text" name="username" lay-verify="username"
					placeholder="请输入手机号" autocomplete="off" class="layui-input">
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">密码</label>
			<div class="layui-input-block">
				<input type="password" name="password" placeholder="请输入密码"
					lay-verify="pass" autocomplete="off" class="layui-input">
			</div>
		</div>

		<div class="layui-form-item">
			<div class="layui-input-block">
				<button class="layui-btn" type="button" lay-submit="" lay-filter="demo1">立即提交</button>
				<button type="reset" class="layui-btn layui-btn-primary">重置</button>
			</div>
		</div>
	</form>
	<script src="../../../static/layui2/layui.js" charset="utf-8"></script>
	<script type="text/javascript" src="../../../static/js/queryUtils.js"></script>
	<script>
		layui.use([ 'jquery','form', 'layedit', 'laydate', 'upload' ],
						function() {
							var form = layui.form
							, layer = layui.layer
							, layedit = layui.layedit
							, laydate = layui.laydate
							, $ = layui.jquery
							, upload = layui.upload;

							var operateType = QueryUtils.GetQueryString("type");
							var id = QueryUtils.GetQueryString("id");
							var active = {
									cancel: function () {
										// 当你在iframe页面关闭自身时
										var index = parent.layer.getFrameIndex(window.name); // 先得到当前iframe层的索引
										parent.layer.close(index); // 再执行关闭
									}
								};
							
							$('.operate-button-group .layui-btn').on('click', function () {
								var type = $(this).data('type');
								active[type] ? active[type].call(this) : '';
							});
							
							//自定义验证规则
							form.verify({
								username : function(value) {
									if (value.length < 11) {
										return '请输入11位数的手机号';
									}
								},
								pass : [ /^[\S]{6,12}$/, '密码必须6到12位，且不能出现空格' ],
								content : function(value) {
									layedit.sync(editIndex);
								}
							});
							

							//监听提交
							form.on('submit(demo1)', function(data) {
								if (operateType === "add") {
									layer.msg('确认保存信息吗', {
										time: 0
										, btn: ['确定', '取消']
										, shade: 0.3
										, yes: function (index) {
											layer.close(index); // 关闭当前提示框
											$.ajax({
												url: "/registerController/addRegister",
												data: data.field,
												dataType: "json",
												type: "post",
												async: false,
												success: function (res) {
												    debugger
													if (res.code === "00") {
														var i = parent.layer.getFrameIndex(window.name);
														parent.layer.close(i);  // 关闭新建页面
														parent.layui.table.reload('dataTable', {});    // 父页面表格刷新
														parent.layer.msg("注册成功", {offset: '150px'});   // 父页面弹出保存成功
													} else if (res.code === "01") {
														layer.msg(res.msg, function () {
														});
													}
												},
												error: function (XMLHttpRequest, textStatus, errorThrown) {
													console.error(XMLHttpRequest.status);
													console.error(XMLHttpRequest.readyState);
													console.error(textStatus);
												}
											});
										}
									});
								}
								else if (operateType === "edit") {
									data.field.id = id;
									layer.msg('确认保存修改后的信息吗', {
										time: 0
										, btn: ['确定', '取消']
										, shade: 0.3
										, yes: function (index) {
											layer.close(index); // 关闭当前提示框
											$.ajax({
												url: "/registerController/updateRegister",
												data: data.field,
												dataType: "json",
												type: "post",
												async: false,
												success: function (res) {
				
													if (res.code === "00") {
														var i = parent.layer.getFrameIndex(window.name);
														parent.layer.close(i);  // 关闭新建页面
														parent.layui.table.reload('dataTable', {});    // 父页面表格刷新
														parent.layer.msg("保存成功", {offset: '150px'});   // 父页面弹出保存成功
													} else if (res.code === "01") {
														layer.msg(res.msg, function () {
														});
													}
												},
												error: function (XMLHttpRequest, textStatus, errorThrown) {
													console.error(XMLHttpRequest.status);
													console.error(XMLHttpRequest.readyState);
													console.error(textStatus);
												}
											});
										}
									});
								}		
								return false;
							});

							//普通图片上传
							  var uploadInst = upload.render({
							    elem: '#uploadFile'
							    ,url: '/registerController/upload/'
							    ,before: function(obj){
							      //预读本地文件示例，不支持ie8
							      obj.preview(function(index, file, result){
							        $('#uploadFile').attr('src', result); //图片链接（base64）
							      });
							    }
							    ,done: function(res){
							      //如果上传失败
							      if(res.code = 0){
							        return layer.msg('上传失败');
							      }else{
										//alert(res.imgname);
										$('#headImg').val("");//如果成功把原来的清空
										$('#headImg').val(res.imgname);
									}
							      //上传成功
							    }
							  });
							
							switch (operateType) {
							case "edit":
								$.ajax({
									url: "/registerController/showUser",
									data: {id: id},
									dataType: "json",
									type: "post",
									async: false,
									success: function (res) {
										debugger
										if (res.code === "00") {
											//表单初始赋值
											form.val('example', {
												"username": res.data.username
												,"headImg": res.data.headImg//这是头像的名称
											})
											form.render();
											
											$("#uploadFile").attr("src",res.uploadFile);
											
										} else if (res.code === "01") {
											layer.msg(res.msg, function () {
											});
										}
									},
									error: function (XMLHttpRequest, textStatus, errorThrown) {
										console.error(XMLHttpRequest.status);
										console.error(XMLHttpRequest.readyState);
										console.error(textStatus);
									}
								});
								break;
							}
							  
						});
	</script>

</body>
</html>